package com.hangzhou.welbeing.welbeinginstrument.fragement;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.activity.AllCircleActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.EveryDayArticlesListActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.ForYourCustomActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.NewDateAritclePagerActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.SimilarProfilePageFocusedActivity;
import com.hangzhou.welbeing.welbeinginstrument.adapter.DailyReadingPagerAdapter;
import com.hangzhou.welbeing.welbeinginstrument.adapter.HeadlinesAdapter;
import com.hangzhou.welbeing.welbeinginstrument.adapter.ReadingEveryDayAdapter;
import com.hangzhou.welbeing.welbeinginstrument.adapter.SmallModuleAdapter;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.AttentionPostBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.CancelAttentionPostBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.ForYourCustomDataBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.GuessYouLikeDateBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.NewDateBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.ReadingEveryDayDateBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideImageLoader;
import com.youth.banner.Banner;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 08 月 21.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.fragement
 * 工程名 welbeing_pregnant
 *
 * @desc 主页Fragment
 */
public class HomePageFragment extends Fragment {

    private static final String TAG = "HomePageFragment";
    @Bind(R.id.fragment_home_page_redesign_tv_weeks_day)
    TextView fragmentHomePageRedesignTvWeeksDay;
    @Bind(R.id.fragment_home_page_redesign_iv_right)
    ImageView fragmentHomePageRedesignIvRight;
    @Bind(R.id.fragment_home_page_redesign_iv_left)
    ImageView fragmentHomePageRedesignIvLeft;
    @Bind(R.id.fragment_home_page_redesign_iv_now_day)
    ImageView fragmentHomePageRedesignIvNowDay;
    @Bind(R.id.fragment_home_page_redesign_title_weeks_right_and_left)
    RelativeLayout fragmentHomePageRedesignTitleWeeksRightAndLeft;
    @Bind(R.id.fragment_home_page_redesign_tv_month_day)
    TextView fragmentHomePageRedesignTvMonthDay;
    @Bind(R.id.fragment_home_page_redesign_iv_Mother_image)
    ImageView fragmentHomePageRedesignIvMotherImage;
    @Bind(R.id.fragment_home_page_redesign_tv_baby_desc)
    TextView fragmentHomePageRedesignTvBabyDesc;
    @Bind(R.id.fragment_home_page_redesign_rl_desc)
    RelativeLayout fragmentHomePageRedesignRlDesc;
    @Bind(R.id.fragment_home_page_redesign_tv_height)
    TextView fragmentHomePageRedesignTvHeight;
    @Bind(R.id.fragment_home_page_redesign_tv_height_specific)
    TextView fragmentHomePageRedesignTvHeightSpecific;
    @Bind(R.id.fragment_home_page_redesign_tv_From_the_baby_was_born)
    TextView fragmentHomePageRedesignTvFromTheBabyWasBorn;
    @Bind(R.id.fragment_home_page_redesign_tv_day_specific)
    TextView fragmentHomePageRedesignTvDaySpecific;
    @Bind(R.id.fragment_home_page_redesign_tv_weight)
    TextView fragmentHomePageRedesignTvWeight;
    @Bind(R.id.fragment_home_page_redesign_tv_weight_specific)
    TextView fragmentHomePageRedesignTvWeightSpecific;
    @Bind(R.id.fragment_home_page_redesign_rl_pregnant_mother)
    RelativeLayout fragmentHomePageRedesignRlPregnantMother;
    @Bind(R.id.fragment_home_page_redesign_recyclerView)
    RecyclerView fragmentHomePageRedesignRecyclerView;
    @Bind(R.id.fragment_home_page_redesign_banner)
    Banner fragmentHomePageRedesignBanner;
    @Bind(R.id.fragment_home_page_redesign_tv_titiles)
    TextView fragmentHomePageRedesignTvTitiles;
    @Bind(R.id.fragment_home_page_redesign_content)
    TextView fragmentHomePageRedesignContent;
    @Bind(R.id.fragment_home_page_redesign_viewpager)
    ViewPager fragmentHomePageRedesignViewpager;
    @Bind(R.id.fragment_home_page_redesign_recyclerView_01)
    RecyclerView fragmentHomePageRedesignRecyclerView01;
    @Bind(R.id.fragment_home_page_redesign_tv_custom_date)
    TextView fragmentHomePageRedesignCustomDate;

    @Bind(R.id.fragment_home_page_redesign_tv_look_more)
    TextView fragmentHomePageRedesignTvLookMore;

    @Bind(R.id.fragment_home_page_redesign_recyclerview_03)
    RecyclerView fragmentHomePageRedesignRecyclerview03;


    @Bind(R.id.fragment_home_page_redesign_rl_more_circle)
    RelativeLayout fragmentHomePageRedesignRlMoreCircle;
    @Bind(R.id.fragment_home_page_redesign_ll_height)
    LinearLayout fragmentHomePageRedesignLlHeight;

    @Bind(R.id.fragment_home_page_redesign_ll_for_your_custom)
    LinearLayout fragmentHomePageRedesignLlForYourCustom;
    private Context mcontext = MyApplication.getMcontext();

    private List<String> images = new ArrayList<>();
    private HeadlinesAdapter headlinesAdapter;
    private SmallModuleAdapter smallModuleAdapter;
    private List<GuessYouLikeDateBean.DataBean> mlist;
    private Date date;
    private SimpleDateFormat format;
    private SimpleDateFormat formatTTER;
    private SimpleDateFormat formatTTER_01;
    private String id_01;
    private long specifictimeday;
    private int p1 = 1;
    private int lastVisibleItem;
    private DailyReadingPagerAdapter dailyReadingPagerAdapter;
    private ReadingEveryDayAdapter readingEveryDayAdapter;
    private long datefictimeday;
    private int p2 = 1;
    private int lastVisibleItem_01;
    private List<ReadingEveryDayDateBean.DataBean> mlist1;
    private List<NewDateBean.DataBean> datemain = new ArrayList<>();

    private CircleFragment circleFragment1;
    private boolean isloding = true;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(mcontext, R.layout.fragment_home_page_redesign, null);
        ButterKnife.bind(this, view);

        /**-------小分类--------*/
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        fragmentHomePageRedesignRecyclerView.setLayoutManager(layoutManager);


        LinearLayoutManager layoutManager_01 = new LinearLayoutManager(getContext());
        layoutManager_01.setOrientation(LinearLayoutManager.HORIZONTAL);
        fragmentHomePageRedesignRecyclerview03.setLayoutManager(layoutManager_01);

        /**-------新鲜事--------*/
        LinearLayoutManager layoutManager_02 = new LinearLayoutManager(getContext());
        layoutManager_02.setOrientation(LinearLayoutManager.VERTICAL);
        fragmentHomePageRedesignRecyclerView01.setLayoutManager(layoutManager_02);
      /*  headlinesAdapter = new HeadlinesAdapter(getContext());
        fragmentHomePageRedesignRecyclerView01.setAdapter(headlinesAdapter);
*/ headlinesAdapter = new HeadlinesAdapter(mcontext);
        fragmentHomePageRedesignRecyclerView01.setAdapter(headlinesAdapter);
        headlinesAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view) {
                int position = fragmentHomePageRedesignRecyclerView01.getChildAdapterPosition(view);
                Intent intent = new Intent(mcontext, NewDateAritclePagerActivity.class);
                intent.putExtra("srtcle_id", datemain.get(position).id);
                startActivity(intent);
            }

            @Override
            public void onItemFirstButtonClick(View v, int position) {

            }
        });
       /* fragmentHomePageRedesignRecyclerView01.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                //判断RecyclerView的状态 是空闲时，同时，是最后一个可见的ITEM时才加载
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem_01 + 1 == headlinesAdapter.getItemCount()) {
                    //headlinesAdapter.changeMoreStatus(headlinesAdapter.OPPEN_MORE);
                    //设置正在加载更多
                    //headlinesAdapter.changeMoreStatus(headlinesAdapter.LOADING_MORE);
                    MyApplication.getmHandler().post(new Runnable() {
                        @Override
                        public void run() {

                            if(isloding){
                                isloding = false;
                                p2++;
                                // Log.e(TAG, "parseNetworkResponse==p2=====================" + p2);
                                loadNewDate(p2);
                            }


                        }
                    });
                    //headlinesAdapter.changeMoreStatus(headlinesAdapter.PULLUP_LOAD_MORE);
                }

            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //最后一个可见的ITEM
              //  lastVisibleItem_01 = layoutManager.findLastVisibleItemPosition();
                lastVisibleItem_01 = layoutManager.findLastCompletelyVisibleItemPosition();
            }
        });
*/

        SharedPreferences sp = mcontext.getSharedPreferences("second_app_internal_data", 0);
        String user_select_state = sp.getString("user_select_state", "");
        if (user_select_state.equals("1")) {
            fragmentHomePageRedesignRlMoreCircle.setVisibility(View.VISIBLE);
            fragmentHomePageRedesignLlHeight.setVisibility(View.VISIBLE);
        } else if (user_select_state.equals("2")) {
            fragmentHomePageRedesignRlMoreCircle.setVisibility(View.GONE);
            fragmentHomePageRedesignLlHeight.setVisibility(View.GONE);
        }
        date = new Date();
        format = new SimpleDateFormat("y-M-d");
        formatTTER = new SimpleDateFormat("M月d日");
        formatTTER_01 = new SimpleDateFormat("M");
        specifictimeday = System.currentTimeMillis();
        datefictimeday = System.currentTimeMillis();
        fragmentHomePageRedesignTvMonthDay.setText(formatTTER.format(date));
        initData();
        return view;
    }

    /**
     * @desc 加载新鲜事
     */
    private void loadNewDate(int p) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL + RBConstants.NEW)
                .addParams("p", p + "")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse = response.body().string();
                        Log.e(TAG, "parseNetworkResponse========" + parseNetworkResponse);
                        Gson gson = new Gson();
                        final NewDateBean newDateBean = gson.fromJson(parseNetworkResponse, NewDateBean.class);
                        if (newDateBean.errorCode == 200) {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    List<NewDateBean.DataBean>     dates = newDateBean.data;
                                    if (dates != null) {

                                        //datemain.addAll(dates);
                                      headlinesAdapter.addItem(dates);
                                        Log.e(TAG, "parseNetworkResponse==datemain=====================" + datemain.size());
                                    }else {
                                        Log.e(TAG, "parseNetworkResponse==datemain==0===================" );
                                    }
                                }
                            });
                        } else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getContext(), newDateBean.errorMessage, Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.fragment_home_page_redesign_iv_right,
            R.id.fragment_home_page_redesign_iv_left,
            R.id.fragment_home_page_redesign_rl_more_circle,
            R.id.fragment_home_page_redesign_ll_for_your_custom,
            R.id.fragment_home_page_redesign_iv_now_day,
            R.id.fragment_home_page_redesign_tv_look_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fragment_home_page_redesign_iv_right:
                specifictimeday = specifictimeday - (1000 * 60 * 60 * 24 * 30);
                String formation = formatTTER.format(specifictimeday);
                fragmentHomePageRedesignTvMonthDay.setText(formation);
                SharedPreferences sp = mcontext.getSharedPreferences("second_app_internal_data", 0);
                String user_set_due_date = sp.getString("user_set_due_date", "2017-10-30");
                loadForYourCustomData(user_set_due_date, format.format(specifictimeday));
               String  NOW = formatTTER_01.format(datefictimeday);

               String  TOW = formatTTER_01.format(specifictimeday);
                if (Integer.parseInt(NOW) > Integer.parseInt(TOW)) {
                    fragmentHomePageRedesignIvNowDay.setVisibility(View.GONE);
                } else {
                    if (datefictimeday >= specifictimeday) {
                        fragmentHomePageRedesignIvRight.setVisibility(View.GONE);
                    } else {
                        fragmentHomePageRedesignIvRight.setVisibility(View.VISIBLE);
                    }
                    fragmentHomePageRedesignIvNowDay.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.fragment_home_page_redesign_iv_left:
                specifictimeday = specifictimeday + (1000 * 60 * 60 * 24 * 30);
                String formation_01 = formatTTER.format(specifictimeday);
                fragmentHomePageRedesignTvMonthDay.setText(formation_01);
                SharedPreferences sp_01 = mcontext.getSharedPreferences("second_app_internal_data", 0);
                String user_set_due_date_01 = sp_01.getString("user_set_due_date", "2017-10-30");
                String  NOW01 = formatTTER_01.format(datefictimeday);

                String  TOW01 = formatTTER_01.format(specifictimeday);
                if (Integer.parseInt(NOW01) > Integer.parseInt(TOW01)) {
                    fragmentHomePageRedesignIvNowDay.setVisibility(View.GONE);
                } else {
                    long non = datefictimeday;
                    if (non == specifictimeday) {
                        fragmentHomePageRedesignIvRight.setVisibility(View.GONE);
                    } else {
                        fragmentHomePageRedesignIvRight.setVisibility(View.VISIBLE);
                    }
                    fragmentHomePageRedesignIvNowDay.setVisibility(View.VISIBLE);
                }
                loadForYourCustomData(user_set_due_date_01, format.format(specifictimeday));
                break;
            case R.id.fragment_home_page_redesign_rl_more_circle:
                Intent intent1 = new Intent(getContext(), AllCircleActivity.class);
                startActivity(intent1);
               // circle();
                break;
            case R.id.fragment_home_page_redesign_ll_for_your_custom: //为你定制
                Intent intent = new Intent(getContext(), ForYourCustomActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_page_redesign_iv_now_day:
                SharedPreferences sp_02 = mcontext.getSharedPreferences("second_app_internal_data", 0);
                String user_set_due_date_02 = sp_02.getString("user_set_due_date", "");
                loadForYourCustomData(user_set_due_date_02, format.format(datefictimeday));
                break;
            case R.id.fragment_home_page_redesign_tv_look_more:
               /* Intent intent07 = new Intent(mcontext, ArticlesActivity.class);
                startActivity(intent07);*/
                Intent intent07 = new Intent(mcontext, EveryDayArticlesListActivity.class);
                startActivity(intent07);
            break;
        }
    }
    /**
     * @desc
     *圈子Fragment
     * */
    private void circle() {
        FragmentManager fragmentManager =getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        /**
         * 为了防止重叠，需要点击之前先移除其他Fragment
         */
        if(circleFragment1 != null){
            transaction.remove(circleFragment1);
        }
        circleFragment1 = new CircleFragment();
        transaction.replace(R.id.activity_main_fl_01, circleFragment1);
        transaction.commit();
    }

    /**
     * @desc 数据加载
     */
    private void initData() {
        /**-------为你定制--------*/
        SharedPreferences sp = mcontext.getSharedPreferences("second_app_internal_data", 0);
        String user_set_due_date = sp.getString("user_set_due_date", "2017-10-30");
        loadForYourCustomData(user_set_due_date, format.format(date));
        /**-------每日一读--------*/
        loadReadingEveryDayDate(1);
        /**-------猜你喜欢--------*/
        loadGuessYouLikeDate(1, 3);
        /**-------加载新鲜事--------*/
        loadNewDate(p2);

    }

    /***
     * @desc 每日一读
     */
    private void loadReadingEveryDayDate(int p) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL + RBConstants.READING_EVERY_DAY)
                .addParams("p", 1 + "")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse = response.body().string();
                        // Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson = new Gson();
                        final ReadingEveryDayDateBean readingEveryDayDateBean = gson.fromJson(parseNetworkResponse, ReadingEveryDayDateBean.class);
                        if (readingEveryDayDateBean.errorCode == 200) {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    mlist1 = readingEveryDayDateBean.data;
                                    if (mlist1 != null) {
                                        /**-------每日一读--------*/
                                        readingEveryDayAdapter = new ReadingEveryDayAdapter(mlist1, mcontext);
                                        fragmentHomePageRedesignRecyclerview03.setAdapter(readingEveryDayAdapter);
                                    }
                                }
                            });
                        } else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getContext(), readingEveryDayDateBean.errorMessage, Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    /**
     * @param user_set_due_date
     * @param format
     * @desc 为你定制
     */
    private void loadForYourCustomData(String user_set_due_date, String format) {
        SharedPreferences sp = mcontext.getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id", "");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL + RBConstants.FOR_YOUR_CUSTOM)
                .addParams("care_date",user_set_due_date)
                .addParams("d", format)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse = response.body().string();
                       // Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson = new Gson();
                        final ForYourCustomDataBean forYourCustomDataBean = gson.fromJson(parseNetworkResponse, ForYourCustomDataBean.class);
                        if (forYourCustomDataBean.errorCode == 200) {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    ForYourCustomDataBean.DataBean mlist = forYourCustomDataBean.data;
                                    if (mlist != null) {
                                        fragmentHomePageRedesignTvHeightSpecific.setText(mlist.height + "cm");
                                        fragmentHomePageRedesignTvDaySpecific.setText(mlist.day + "天");
                                        fragmentHomePageRedesignTvWeightSpecific.setText(mlist.weight + "g");
                                        Glide.with(mcontext).load(RBConstants.SERVER_URL + "/Public/Uploads/" + mlist.url).transform(new GlideCircleTransform(mcontext)).into(fragmentHomePageRedesignIvMotherImage);
                                        fragmentHomePageRedesignTvBabyDesc.setText(mlist.child_change);
                                        fragmentHomePageRedesignTvWeeksDay.setText(mlist.care_day / 7 + "周" + mlist.care_day % 7 + "天");
                                        fragmentHomePageRedesignCustomDate.setText(mlist.day / 7 + "周" + mlist.day % 7 + "天");
                                        fragmentHomePageRedesignContent.setText(mlist.art);
                                        List<ForYourCustomDataBean.DataBean.AdBean> art_url = mlist.ad;
                                        for (ForYourCustomDataBean.DataBean.AdBean adBean : art_url) {
                                            images.add(RBConstants.SERVER_URL + "/Public/Uploads/" + adBean.ad_url);
                                        }
                                        fragmentHomePageRedesignBanner.setImages(images).setImageLoader(new GlideImageLoader()).start();

                                    }
                                }
                            });
                        } else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getContext(), forYourCustomDataBean.errorMessage, Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 加载猜你喜欢
     */
    private void loadGuessYouLikeDate(final int p, int pp) {
        SharedPreferences sp = mcontext.getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id", "");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL + RBConstants.RECOMMEND_POST)
                .addParams("cus_id", user_id)
                .addParams("p", p + "")
                .addParams("pp", pp + "")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse = response.body().string();
                        // Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson = new Gson();
                        final GuessYouLikeDateBean guessYouLikeDateBean = gson.fromJson(parseNetworkResponse, GuessYouLikeDateBean.class);
                        if (guessYouLikeDateBean.errorCode == 200) {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    mlist = guessYouLikeDateBean.data;
                                    if (mlist != null) {
                                        smallModuleAdapter = new SmallModuleAdapter(mlist, mcontext);
                                        fragmentHomePageRedesignRecyclerView.setAdapter(smallModuleAdapter);
                                        smallModuleAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                                            @Override
                                            public void onItemClick(View view) {
                                                int position = fragmentHomePageRedesignRecyclerView.getChildAdapterPosition(view);
                                                Intent intent = new Intent(getContext(), SimilarProfilePageFocusedActivity.class);
                                                intent.putExtra("po_id", mlist.get(position).po_id);
                                               // intent.putExtra("po_url", mlist.get(position).po_url);
                                                intent.putExtra("po_name", mlist.get(position).po_name);
                                                intent.putExtra("content", mlist.get(position).content);
                                                intent.putExtra("cus_logo", mlist.get(position).cus_logo);
                                                intent.putExtra("addtime", mlist.get(position).addtime);
                                                intent.putExtra("cus_name", mlist.get(position).cus_name);
                                                intent.putExtra("state", mlist.get(position).state + "");
                                                intent.putExtra("count", mlist.get(position).count + "");
                                                intent.putExtra("cus_id", mlist.get(position).cus_id + "");
                                                intent.putExtra("col", mlist.get(position).col + "");
                                                intent.putExtra("lCount", mlist.get(position).lCount + "");
                                                intent.putExtra("lstate", mlist.get(position).lstate + "");
                                                intent.putExtra("hits_like", mlist.get(position).hits_like + "");
                                                startActivity(intent);
                                            }

                                            @Override
                                            public void onItemFirstButtonClick(View v, int position) {
                                                String cus_id = mlist.get(position).cus_id;
                                                int state = mlist.get(position).state;
                                                if (state == 0) {
                                                    addAttentionPost(cus_id);
                                                } else if (state == 1) {
                                                    cancelAttentionPost(cus_id);
                                                }
                                            }
                                        });

                                    }
                                }
                            });
                        } else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getContext(), guessYouLikeDateBean.errorMessage, Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    /**
     * @param
     * @desc 取消关注帖子
     */
    private void cancelAttentionPost(String cus_id) {
        SharedPreferences sp = mcontext.getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id", "");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL + RBConstants.CANCLE_ATTENTION_POST)
                .addParams("cus_id", user_id)
                .addParams("followed", cus_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse = response.body().string();
                        //  Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson = new Gson();
                        final CancelAttentionPostBean cancelAttentionPostBean = gson.fromJson(parseNetworkResponse, CancelAttentionPostBean.class);
                        if (cancelAttentionPostBean.errorCode == 200) {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getContext(), "取消关注成功", Toast.LENGTH_LONG).show();
                                    loadGuessYouLikeDate(1, 3);
                                }
                            });
                        } else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getContext(), cancelAttentionPostBean.errorMessage, Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @param
     * @desc 添加关注帖子
     */
    private void addAttentionPost(String cus_id) {
        SharedPreferences sp = mcontext.getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id", "");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL + RBConstants.ADD_ATTENTION_POST)
                .addParams("cus_id", user_id)
                .addParams("followed", cus_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse = response.body().string();
                        //Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson = new Gson();
                        final AttentionPostBean attentionPostBean = gson.fromJson(parseNetworkResponse, AttentionPostBean.class);
                        if (attentionPostBean.errorCode == 200) {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getContext(), "关注成功", Toast.LENGTH_LONG).show();
                                    loadGuessYouLikeDate(1, 3);

                                }
                            });
                        } else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getContext(), attentionPostBean.errorMessage, Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }


}
